package com.gexin.model.system;

import java.util.List;

import com.gexin.common.utils.SQLUtils;
import com.gexin.common.utils.StringUtil;
import com.gexin.model.system.base.BaseAdminLog;
import com.jfinal.plugin.activerecord.Page;

/**
 * 业务名称:  t_admin_log(系统日志信息) 
 */
@SuppressWarnings("serial")
public class AdminLog extends BaseAdminLog<AdminLog> {
	public static final AdminLog dao = new AdminLog();
	/**
	 * 日志添加 记得去字典添加对应的值
	 * @author 肖得明
	 *
	 */
	public static enum Log_Type {
		/**登录日志**/
		LOGIN,
		/**一般日志**/
		LOG_INFO,
		/**警告日志**/
		LOG_WARING, 
		/**错误日志**/
		LOG_ERROR, 
		/**操作日志**/
		Operation
	}
	
	/**
	 * 添加日志
	 * @param type
	 * @param user_id
	 * @param info
	 * @return
	 */
	public boolean addLog(Log_Type type,Integer user_id,String info){
		AdminLog log  =new AdminLog();
		log.setLogInfo(info);
		log.setUserId(user_id);
		log.setLogType(type.toString());
		return log.save();
	}
	
	/**
	 * 添加日志
	 * @param live   日志类型
	 * @param user_id  用户ID
	 * @param info   日志信息
	 * @param data   日志的数据
	 * @return
	 */
	public boolean addLog(Log_Type type,Integer user_id,String info,String data){
		AdminLog log  =new AdminLog();
		log.setLogInfo(info);
		log.setUserId(user_id);
		log.setLogType(type.toString());
		log.setLogData(data);
		return log.save();
	}
	
	//查询log 分页
	public Page<AdminLog> paginate(int pageNumber, int pageSize,AdminLog log) {
		SQLUtils sql  = new SQLUtils(" from  t_admin_log as log  LEFT JOIN  t_admin_user  as u  on log.user_id=u.id  where 1=1  ");
		sql.append(build_query_sql(log));
		sql.append("order by log.id  desc ");
		String select_sql = "select log.*,u.name as user_relName,u.username,(select tmp.busi_name from t_admin_dict tmp where tmp.busi_value=log.log_type and tmp.parent_id=(select id from t_admin_dict where busi_value='LOG_TYPE')) as log_type_text";
		return paginate(pageNumber, pageSize, select_sql, sql.toString());
	}
	
	public  String build_query_sql(AdminLog log){
		SQLUtils sql  = new SQLUtils();
		if(log!=null){
			if(StringUtil.isEmpty(log.getLogType())==false){
				sql.whereEquals("log.log_type", log.getLogType());
			}
			
			if(StringUtil.isEmpty(log.getLogInfo())==false){
				sql.append(" and log.log_info like '%"+log.getLogInfo()+"%'");
			}
			//时间查询
			if(StringUtil.isNotBlank(log.getStr("startdate")) && StringUtil.isNotBlank(log.getStr("enddate"))){
				sql.append(String.format(" and log.createtime BETWEEN '%s' and '%s'", log.getStr("startdate"),log.getStr("enddate")));
			}
		}
		return  sql.toString();
	}
	
	public List<AdminLog> queryLogList(AdminLog log) {
		SQLUtils sql  = new SQLUtils("select log.*,u.username,u.name as user_relName,(select tmp.busi_name from t_admin_dict tmp where tmp.busi_value=log.log_type and tmp.parent_id=(select id from t_admin_dict where busi_value='LOG_TYPE')) as log_type_text from  t_admin_log as log  LEFT JOIN  t_admin_user  as u  on log.user_id=u.id where 1=1");
		sql.append(build_query_sql(log));
		sql.append(" order by log.id  desc ");
		return dao.find(sql.toString());
	}
	
	
}
